#include <bits/stdc++.h>
using namespace std;

class Solution
{
public:
    bool check[25][25];
    int dx[4] = {-1, 1, 0, 0};
    int dy[4] = {0, 0, -1, 1};
    int n, m;
    int ret = 0;
    vector<vector<int>> gridhelp;
    bool help()
    {
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (gridhelp[i][j] == 0 && check[i][j] == false)
                { 
                    return false;
                }
            }
        }
        return true;
    }
    void dfs(int i, int j)
    {
        if (gridhelp[i][j] == 2)
        {
            if (help())
            {
                ret++;
            }
            return;
        }
        for (int k = 0; k < 4; k++)
        {
            int x = dx[k] + i;
            int y = dy[k] + j;
            if(x>=0&&x<n&&y>=0&&y<m&&check[x][y]==false&&gridhelp[x][y]!=-1)
            {
                check[x][y]=true;
                dfs(x,y);
                check[x][y]=false;
            }
        }
    }
    int uniquePathsIII(vector<vector<int>> &grid)
    {
        n = grid.size();
        m = grid[0].size();
        gridhelp = grid;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (grid[i][j] == 1)
                {
                    check[i][j] = true;
                    dfs(i, j);
                }
            }
        }
        return ret;
    }
};